package dearchenpush.good.com.qianfanlive.modules.discover.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.util.List;

import dearchenpush.good.com.qianfanlive.modules.discover.bean.ViewPagerBean;

/**
 * User: sgd(952519440@qq.com)
 * Date: 2016/09/28
 * Time: 11:44
 * progject: QianfanLive
 * Describe：
 */

public class ViewPagerHeadAdapter extends PagerAdapter {

    private List<ViewPagerBean.MessageBean.BannersBean> mData;
    private Context mContext;

    public ViewPagerHeadAdapter(List<ViewPagerBean.MessageBean.BannersBean> url, Context context) {
        this.mData = url;
        mContext = context;
    }

    @Override
    public int getCount() {
        //设置成最大，使用户看不到边界
        return mData.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
//        if (mData.size() > 0) {
//            if (position >= mData.size()) {
//                position = position % mData.size();
//            }



            ImageView imageView = new ImageView(mContext);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            Picasso.with(mContext).load(mData.get(position).getPicUrl()).into(imageView);

            //如果View已经在之前添加到了一个父组件，则必须先remove，否则会抛出IllegalStateException。
//            ViewParent vp = imageView.getParent();
//            if (vp != null) {
//                ViewGroup parent = (ViewGroup) vp;
//                parent.removeView(imageView);
//            }

            container.addView(imageView);
            return imageView;
//        } else {
//            return null;
//        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (object instanceof View) {
            View view = (View) object;
            container.removeView(view);
        }
    }
}
